package dao;


import entity.ChessUser;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 调用数据库语言，操作ChessUser类.
 *
 * @author 任禹名
 */
public class ChessUserDao {
    String sql = null;
    BaseDao baseDao = new BaseDao();

    /**
     * 查找玩家
     *
     * @param userName 玩家姓名
     * @return 返回实体类供他人调用
     */
    public ChessUser findUser(String userName) {
        sql = "select * from ChessUser where name=?";
        String params[] = {userName};
        ResultSet resultSet = baseDao.exeQuery(sql, params);
        ChessUser chessUser = null;
        try {
            while (resultSet.next()) {
                chessUser = new ChessUser(resultSet.getString(1), resultSet.getString(2));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return chessUser;
    }

    /**
     * 功能: 查找玩家信息
     *
     * @return 返回实体类供他人调用
     */
    public List<ChessUser> findAll() {
        // TODO Auto-generated method stub
        String sql = "select * from ChessUser";
        ResultSet resultSet = baseDao.exeQuery(sql);
        ArrayList list = new ArrayList();
        ChessUser chessUser = null;
        try {
            while (resultSet.next()) {
                chessUser = new ChessUser(resultSet.getString(1), resultSet.getString(2));
                list.add(chessUser);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 功能:删除玩家
     *
     * @param userName 玩家姓名
     */
    public void deleteUser(String userName) {
        sql = "delete from ChessUser where name=?";
        String[] params = {userName};
        baseDao.exeUpdate(sql, params);

    }

    /**
     * 功能: 设置玩家的头像
     *
     * @param fileName 头像路径
     */
    public void updateUserImg(String fileName, String name) {
        sql = "update chessUser set fileName=?  where name=? ";
        String[] params = {fileName, name};
        baseDao.exeUpdate(sql, params);
    }

    public void insertUser(ChessUser user) {
        sql = "insert into ChessUser(name) values(?)";
        System.out.println("新增用户");
        String[] params = {user.getName()};
        baseDao.exeUpdate(sql, params);
    }
}
